
angular.module('mamApp').controller('userSignController', ['$scope', 'AuthManager', '$state','ImsAlert','ImsErr',
    function ($scope, AuthManager, $state,ImsAlert,ImsErr) {
    $scope.agree=false;
    $scope.disabled=false;
    $scope.emailError_a=true;
    $scope.passwordDisabled=false;
    $scope.user.redirectUrl='http://192.168.2.207/MAM_AP_WEB_V1/public/#/user/sign?step=3';
    var mailCheck=$state.params.mailCheck;
    $scope.step=$state.params.step||1;

        console.log($scope.agree);
    $scope.agreement=false;
    $scope.error=true;

        $scope.mailbox = function () {
            $scope.disabled=true;
            AuthManager.mailbox({userName:$scope.user.userName,redirectUrl: $scope.user.redirectUrl}).then(function (result) {
                $scope.disabled=false;
                if(result.data.c=="s"){
                    $state.go($state.current.name,{step:2})
                }else {
                    $scope.emailError_a=true;
                    $scope.emailError=result.data.i;
                }
            })
        }

     $scope.ment=function(){
         $scope.agree=true;
     }
    $scope.protocol=function(){
        $scope.agree=false;
        $scope.agreement=true;
    }

  $scope.sign=function(){
      $scope.passwordDisabled=true;
      AuthManager.mailCheck({mailCheck:mailCheck}).then(function(result){

          if(result.data.c=="s"){
              $scope.passwordDisabled=false;
              localStorage.setItem("userInfo",JSON.stringify(result.data.userCpVO));
              AuthManager.sign({token:result.data.userCpVO.token,password:$scope.user.password}).then(function(result){
                  if(result.data.c=="s"){
                      localStorage.setItem("userInfo",JSON.stringify(result.data.userCpVO));
                      $state.go("main.success")
                  }
              })
          }else (
              ImsErr.open({message:result.data.i}).then(function (a) {
              })
          )
      })
  }



}])

